﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OthelloGame;

namespace OthelloCognition
{
    public class GreedySolver:AbstractSolver
    {
        AbstractHeuristic h;
        public GreedySolver(Heuristics heur)
        {
            h = HeuristicFactory.getHeuricstic(heur);
        }

        public override SolverEnum SolverName()
        {
            return SolverEnum.GreedyScore;
        }
       
        protected override void examineBoard(OthelloGameBoard board)
        {
            int bestscore = int.MinValue;
            List<OthelloBoardLocation> options = new List<OthelloBoardLocation>();
            foreach (OthelloBoardLocation pit in board.AvailableOptionLocations)
            {
                OthelloGameBoard best = expandState(board, pit);
                int score = h.evaluate(best, board.PlayersTurn);
                if (score > bestscore)
                {
                    move = pit;
                    options.Clear();
                    options.Add(pit);
                    bestscore = score;
                }
                else if (score == bestscore)
                {
                    options.Add(pit);
                }
            }
            Random r = new Random();
            if (options.Count != 0)
            {
                int n = r.Next() % options.Count;
                move = options.ElementAt(n);
            }
        }

        override public void recordResult(bool isWinner)
        {
            //Console.WriteLine("Greedy is a winner? " + isWinner);
        }
    }
}
